<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Reserve extends Model
{
    use HasFactory;

         /**
     * 是否主动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;

     /**
     * table name
     *
     * @var string
     */
    protected $table = 'reserve';

    // time string
    // const CREATED_AT = 'create_time';

    /**
     * 获取该预约的所属用户
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'uid');
    }

    /**
     * 根据用户ID获取该用户的预约数量
     * @param  int $uid 用户ID
     * @return int      数量
     */
    public function getReserveNum($uid)
    {
        $num = $this->where(['uid' => $uid]) -> count();
        return $num;
    }

    /**
     * 根据用户ID获取该用户当天是否存在预约
     * @param  int $uid 用户ID
     * @return 
     */
    public function getReserveExist($uid, $date)
    {
        $data = $this->where(['uid' => $uid, 'reserved_day' => $date]) -> get() -> toArray();
        if (!$data) {
            return false;
        }
        return true;
    }

    /**
     * 获取整场活动的预约数量
     * @return int 数量
     */
    public function getReserveCount()
    {
        $count = $this->count();
        return $count;
    }
}
